package com.epro.dx.activity.order;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.activity.BaseFragmentActivity;
import com.epro.dx.pullview.PullToRefreshBase.Mode;
import com.epro.dx.pullview.PullToRefreshListView;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.facade.model.PackageTrace;

/**
 * @author Tony
 * 物流信息详情页
 */
public class ShippingInformationActivity extends BaseFragmentActivity {
	
	private PullToRefreshListView infoList;
	private RelativeLayout backBt;//返回按钮
	private String trackingNumber;
	private TextView trackingNumberText;
	private ArrayList<PackageTrace> packageTraces;
    private Button copyBtn;
    //复制板
    private ClipboardManager clipBoardManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_shippinginfo);
		sendGA();
		initView();
		initData();
		initEvent();
	}

	/**
	 * GA跟踪
	 */
	private void sendGA() {
		Tracker tracker = DXApplication.getContext().getTracker(TrackerName.APP_TRACKER);
		tracker.setScreenName("Package Tracking");
		tracker.send(new HitBuilders.ScreenViewBuilder().build());
	}

	/**
	 * 初始化事件监听
	 */
	@SuppressLint("NewApi")
	private void initEvent() {
		backBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// 返回上一个页面
				finish();
			}
		});
		
		copyBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				ClipData myClip = ClipData.newPlainText("text", trackingNumber);
				clipBoardManager.setPrimaryClip(myClip);
			}
		});
	}

	/**
	 * 加载数据
	 */
	private void initData() {
		Intent intent = getIntent();
		if(intent!=null){
			Bundle extras = intent.getExtras();
			trackingNumber = extras.getString("TrackingNumber");
			packageTraces = extras.getParcelableArrayList("PackageTrace");
		}
		if(!TextUtils.isEmpty(trackingNumber)){
			trackingNumberText.setText(getString(R.string.shippingTracking_tracking_number)+" "+trackingNumber);
		}
		infoList.setAdapter(new ShippingInfoListAdapter());
	}

	/**
	 * 加载视图
	 */
	private void initView() {
		 clipBoardManager= (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
		copyBtn=(Button) findViewById(R.id.copy_order_number);	
		infoList = (PullToRefreshListView) findViewById(R.id.shippinginfo_listview);
		backBt = (RelativeLayout) findViewById(R.id.topbar_back);
		trackingNumberText = (TextView) findViewById(R.id.shippinginfo_tracking_number);
		
		infoList.setMode(Mode.DISABLED);
	}
	
	/**
	 * @author Tony
	 * 物流信息列表数据适配器
	 */
	class ShippingInfoListAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			if(packageTraces!=null){
				return packageTraces.size();
			}
			return 0;
		}

		@Override
		public PackageTrace getItem(int position) {
			return packageTraces.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null){
				holder = new ViewHolder();
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_shippinginfo_list, null);
				holder.timeLineOn = (View) convertView.findViewById(R.id.item_shippinginfo_timeline_on);
				holder.timeLineOff = (View) convertView.findViewById(R.id.item_shippinginfo_timeline_off);
				holder.timeLineLast = (View) convertView.findViewById(R.id.item_shippinginfo_timeline_last);
				holder.timeLinePointOn = (ImageView) convertView.findViewById(R.id.item_shippinginfo_timeline_point_on);
				holder.timeLinePointOff = (ImageView) convertView.findViewById(R.id.item_shippinginfo_timeline_point_off);
				holder.shippingInfoText = (TextView) convertView.findViewById(R.id.item_shippinginfo_text);
				holder.shippingInfoTime = (TextView) convertView.findViewById(R.id.item_shippinginfo_time);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			//设置时间轴的显示
			if(position==0){
				holder.timeLineOn.setVisibility(View.VISIBLE);
				holder.timeLineOff.setVisibility(View.GONE);
				holder.timeLineLast.setVisibility(View.GONE);
				holder.timeLinePointOn.setVisibility(View.VISIBLE);
				holder.timeLinePointOff.setVisibility(View.GONE);
			}else if(packageTraces.size()>1&&position == (packageTraces.size()-1)){
				holder.timeLineOn.setVisibility(View.GONE);
				holder.timeLineOff.setVisibility(View.GONE);
				holder.timeLineLast.setVisibility(View.VISIBLE);
				holder.timeLinePointOn.setVisibility(View.GONE);
				holder.timeLinePointOff.setVisibility(View.VISIBLE);
			}else{
				holder.timeLineOn.setVisibility(View.GONE);
				holder.timeLineOff.setVisibility(View.VISIBLE);
				holder.timeLineLast.setVisibility(View.GONE);
				holder.timeLinePointOn.setVisibility(View.GONE);
				holder.timeLinePointOff.setVisibility(View.VISIBLE);
			}
			//设置字体颜色
			if(position==0){
				holder.shippingInfoText.setTextColor(android.graphics.Color.parseColor("#FF6600"));
				holder.shippingInfoTime.setTextColor(android.graphics.Color.parseColor("#FF6600"));
			}else{
				holder.shippingInfoText.setTextColor(android.graphics.Color.parseColor("#999999"));
				holder.shippingInfoTime.setTextColor(android.graphics.Color.parseColor("#999999"));
			}
			//设置数据
			holder.shippingInfoText.setText(packageTraces.get(position).getTrace());
			holder.shippingInfoTime.setText(packageTraces.get(position).getTraceTime());
			return convertView;
		}
		
	}
	class ViewHolder{
		View timeLineOn;//第一条数据需要显示的时间线样式
		View timeLineOff;//非第一条数据需要显示的时间线样式
		View timeLineLast;//最后一条数据需要显示的时间线样式
		ImageView timeLinePointOn;//橙色圆点
		ImageView timeLinePointOff;//灰色圆点
		TextView  shippingInfoText;//物流信息文本
		TextView  shippingInfoTime;//物流信息时间信息
	}
}
